package com.pet.server.ext.action.request;

import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.pet.server.ext.ActionBase;
import com.pet.server.ext.ApiError;
import com.pet.server.ext.BeanToJson;
import com.pet.server.ext.Result;
import com.pet.server.ext.constants.BaseAttribute;
import com.pet.server.ext.data.AnswerInfo;
import com.pet.server.ext.data.Option;
import com.pet.server.ext.data.RequestInfo;
import com.pet.server.ext.service.UserService;
import com.pet.server.ext.util.NumberUtil;
import com.pet.server.ext.util.Parameters;
import com.pet.server.ext.util.StringUtil;

/**
 * 
 * */
public class Detail extends ActionBase{


	public Integer id;
	public String detail;
	public Detail(){
		 needCheckAuth =true;
	}
	
	@Override
	public void initParameters(Parameters params) throws ApiError {
		id = NumberUtil.getInteger(params.getFieldValue("id"));
		detail = StringUtil.dealNull(params.getFieldValue("detail"),"1");
		if(StringUtil.isEmpty(id)){
			throw ApiError.Type.INVALID_PARAM.toException();
		}
	}
	
	@Override
	public Result invokeService() throws ApiError {
		UserService userService = new UserService();
		RequestInfo req = userService.findRequestInfo(id);
		if(null==req){
			throw ApiError.Type.LOGIC_ERROR.toException("提问或调查不存在!");
		}
		JSONArray array = new JSONArray();
		if(BaseAttribute.diaoc.equals(req.type)){
      	  List<Option> list = userService.queryOptionByRequestId(req.id);
      	  array = BeanToJson.listTojson(list);
        }else if(BaseAttribute.tiw.equals(req.type)){
          List<AnswerInfo> list = userService.queryAnswerInfo(req.getId(),BaseAttribute.answer_type.request.toString());
          array = BeanToJson.listTojson(list);
        }
		JSONObject o = new JSONObject();
		if(StringUtil.equals(BaseAttribute.LOGIC_TRUE, detail)){
			o.put("detail", BeanToJson.beanToJson(req,"answer_id"));
		}
		o.put("list", array);
		Result result = new Result();
		result.put("data",o);
	    return result;
	}



}
